﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.Core.Documentset;
using Office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint.Linq;

namespace MS.InformationManagement.BLL.AnulacionRadicado
{
    public class AnulacionRadicado
    {

        public static bool RechazarSolicitud(int itemID, string ObservacionesAdmin)
        {
            bool result = false;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                    using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                    {
                        using (SPWeb currentWeb = currentSite.OpenWeb())
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            SPList listAnulados = currentWeb.Lists.TryGetList(GlobalConstants.LIST_SOLICITUD_ANULADOS);
                            SPListItem newItem = listAnulados.Items.GetItemById(itemID);
                            newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_OBSERVACIONES_ADMIN] = ObservacionesAdmin;
                            newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_ESTADO] = Estado.Rechazada.ToString();
                            newItem.Update();
                            result = UpdateRadicado(newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_LIST_TARGET] + "", newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_ID_RADICADO_INTERNO] + "", ObservacionesAdmin, GlobalConstants.METADATA_TERM_STATUS_ANULADO);
                            currentWeb.AllowUnsafeUpdates = false;

                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        public static bool AceptarSolicitud(int itemID, string ObservacionesAdmin)
        {
            bool result = false;

            try
            {
                string LoginName = SPContext.Current.Web.CurrentUser.LoginName;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                    using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                    {
                        using (SPWeb currentWeb = currentSite.OpenWeb())
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            SPList listAnulados = currentWeb.Lists.TryGetList(GlobalConstants.LIST_SOLICITUD_ANULADOS);
                            SPListItem newItem = listAnulados.Items.GetItemById(itemID);
                            newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_OBSERVACIONES_ADMIN] = ObservacionesAdmin;
                            newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_ESTADO] = Estado.Aprobada.ToString();
                            newItem.Update();
                            result = UpdateRadicadoAceptarSolicitud(newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_LIST_TARGET] + "", newItem[CustomSiteColumns.FIELD_SOLICITUD_ANULACION_ID_RADICADO_INTERNO] + "", ObservacionesAdmin, GlobalConstants.METADATA_TERM_STATUS_ANULADO, LoginName);
                            currentWeb.AllowUnsafeUpdates = false;
                            
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        public static bool UpdateRadicado(string ListTarget, string ItemListID, string ObservacionesAdmin, string RadicadoEstado)
        {
            bool result = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                 SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                 using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                 {
                     using (SPWeb currentWeb = currentSite.OpenWeb())
                     {
                         SPList ListCurrentRadicado = currentWeb.Lists[ListTarget];
                         int IdInternoRadicado = int.Parse(ItemListID);

                         Office.DocumentSet CurrentDocumentSet = DocumentSetHelper.GetDocumentSet(ListCurrentRadicado, IdInternoRadicado);
                         currentWeb.AllowUnsafeUpdates = true;
                         CurrentDocumentSet.Folder.Update();
                         CurrentDocumentSet.Folder.Item.Update();

                         CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_OBSERVACIONES_ADMIN] = ObservacionesAdmin;
                         TaxonomyFieldValue TaxValue = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_STATUS, RadicadoEstado);
                         CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = TaxValue;
                         CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_0] = TaxValue;

                         CurrentDocumentSet.Folder.Item.Update();
                         CurrentDocumentSet.Folder.Update();
                         currentWeb.AllowUnsafeUpdates = false;
                     }
                 }
    
            });
           
            result = true;

            return result;
        }

        public static bool UpdateRadicadoAceptarSolicitud(string ListTarget, string ItemListID, string ObservacionesAdmin, string RadicadoEstado, string LoginName)
        {
            bool result = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                 using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                 {
                     using (SPWeb currentWeb = currentSite.OpenWeb())
                     {
                         result = UpdateRadicado(ListTarget, ItemListID, ObservacionesAdmin, RadicadoEstado);
                         SPList ListCurrentRadicado = currentWeb.Lists[ListTarget];
                         int IdInternoRadicado = int.Parse(ItemListID);
                         SPList ListaAnulados = currentWeb.Lists.TryGetList(GlobalConstants.LIST_ANULADOS);
                         Office.DocumentSet CurrentDocumentSet = DocumentSetHelper.GetDocumentSet(ListCurrentRadicado, IdInternoRadicado);
                         result = new MS.InformationManagement.BLL.ReasignarDocumento.ReasignarDocumento().MoveRadicadoAnulado(CurrentDocumentSet, ListaAnulados, LoginName);
                     }
                 }
             });
            return result;
        }
    }
}
